package com.wutianyi.jmeter.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

public class CreatePassword implements JavaSamplerClient {

	private ScriptEngineManager manager = new ScriptEngineManager();

	private ScriptEngine engine = manager.getEngineByName("JavaScript");

	private String password = null;

	private String code = null;

	private String verify = null;

	public CreatePassword() {

		try {
			System.out.println(CreatePassword.class.getResource("").getPath());
			URL url = CreatePassword.class
					.getResource("/com/wutianyi/script/phone.js");
			System.out.println("url: " + url.getPath());

			File file = new File(url.getPath());
			if (file.exists()) {
				System.out.println("file exits!");
			} else {
				System.out.println("file no exits!");
			}

			if(engine == null)
			{
				System.out.println("Engine is: null");
			}
			engine.eval(new InputStreamReader(new FileInputStream(file)));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ScriptException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void setupTest(JavaSamplerContext context) {
		password = context.getParameter("password");
		code = context.getParameter("code");
		verify = context.getParameter("verify");
	}

	@Override
	public SampleResult runTest(JavaSamplerContext context) {
		SampleResult results = new SampleResult();
		results.setResponseCodeOK();

		results.setResponseMessage(password + ": " + code + ": " + verify);
		return results;
	}

	@Override
	public void teardownTest(JavaSamplerContext context) {
		// TODO Auto-generated method stub

	}

	@Override
	public Arguments getDefaultParameters() {
		// TODO Auto-generated method stub
		return null;
	}

}
